package binance.controller

import binance.model.*

class InvestmentsController {

    def show = {
        
    }

    def allcurrent = {
        def currentInvestments = null
        if (params['sort'])
            currentInvestments = Buy.currentInvestments(session.user,params['sort'],params['order'])
        else
            currentInvestments = Buy.currentInvestments(session.user)
        def sumInvested = Buy.sumInvested(session.user)
        def sumValue = Buy.sumValue(session.user)
        def sumProfit = Buy.sumProfit(session.user)
        def avgProfitProc = Buy.avgProfitProc(session.user)
        def sumTargetValue = Buy.sumTarget(session.user)
        [currentInvestments:currentInvestments,currentSumInvested:sumInvested,currentSumValue:sumValue,currentSumProfit:sumProfit,currentAvgProfitProc:avgProfitProc,currentSumTargetValue:sumTargetValue]
    }

    def current = {
        def buy = Buy.findById(params['id'])
        [buy : buy]
    }

    def allpast = {
        def sumInvested = Sell.sumInvested(session.user)
        def sumSale = Sell.sumSale(session.user)
        def sumProfit = Sell.sumProfit(session.user)
        def avgProfitProc = Sell.avgProfitProc(session.user)
        [pastInvestments:Sell.getAllSells(session.user),pastSumInvested:sumInvested,pastSumSale:sumSale,pastSumProfit:sumProfit,pastAvgProfitProc:avgProfitProc]
    }

    def past = {
        log.debug "params id: ${params['id']}"
        def sell = Sell.findById(params['id'])
        if (sell.buy.user.id == session.user.id) {
            [sell : sell]
        }else{
            render(text:"Incorrect user")
        }
        [sell : sell]
    }

    def summary= {
        def past = allpast()
        def current = allcurrent()
        past + current
    } 
}
